/**
 * 交易流水管理初始化
 */
var TransHistory = {
    id: "TransHistoryTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
TransHistory.initColumn = function () {
    return [
            {field: 'selectItem', radio: true},
            {title: '商户号', field: 'mchNo', visible: true, align: 'center', valign: 'middle'},
            {title: '交易订单号', field: 'orderCode', visible: true, align: 'center', valign: 'middle'},
            {title: '平台订单号', field: 'platOrderCode', visible: true, align: 'center', valign: 'middle'},
            {title: '订单时间', field: 'orderTime', visible: true, align: 'center', valign: 'middle'},
            {title: '订单金额', field: 'orderAmount', visible: true, align: 'center', valign: 'middle'},
          /*  {title: '真实姓名', field: 'realName', visible: true, align: 'center', valign: 'middle'},
            {title: '身份证号', field: 'idCard', visible: true, align: 'center', valign: 'middle'},
            {title: '支付卡号', field: 'accNo', visible: true, align: 'center', valign: 'middle'},
            {title: '手机号', field: 'mobile', visible: true, align: 'center', valign: 'middle'},*/
            {title: '交易时间', field: 'tranTime', visible: true, align: 'center', valign: 'middle'},
            {title: '通道流水号', field: 'chSerialNo', visible: true, align: 'center', valign: 'middle'},
            {title: '手续费', field: 'tradeFee', visible: true, align: 'center', valign: 'middle'},
            {title: '实收金额', field: 'actualAmount', visible: true, align: 'center', valign: 'middle'},
            {title: '交易状态', field: 'tranStatus', visible: true, align: 'center', valign: 'middle',formatter:"statusFormatter"}    
    ];
};

function statusFormatter(value, row, index){
	if (value == 0) {
		return '<span class="label label-success">成功</span>';
	} else if(value == 1) {
		return '<span class="label label-warning">处理中</span>';
	}else if(value == 2) {
		return '<span class="label label-danger">失败</span>';
	}else if(value == 3) {
		return '<span class="label label-warning">待支付</span>';
	}
}

/**
 * 检查是否选中
 */
TransHistory.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        TransHistory.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加交易流水
 */
TransHistory.openAddTransHistory = function () {
    var index = layer.open({
        type: 2,
        title: '添加交易流水',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/transHistory/transHistory_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看交易流水详情
 */
TransHistory.openTransHistoryDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '交易流水详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/transHistory/transHistory_update/' + TransHistory.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除交易流水
 */
TransHistory.delete = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/transHistory/delete", function (data) {
            Feng.success("删除成功!");
            TransHistory.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.set("transHistoryId",this.seItem.id);
        ajax.start();
    }
};

/**
 * 导出
 */
TransHistory.download = function () {
	var orderCode= $("#orderCode").val();
    var platOrderCode = $("#platOrderCode").val();
    var beginTime = $("#beginTime").val();
    var endTime = $("#endTime").val(); 
    var tranStatus = $("#tranStatus").val(); 
	window.location.href=Feng.ctxPath + "/transHistory/download?orderCode"+orderCode+"&platOrderCode="+platOrderCode+"&beginTime="+beginTime+"&endTime="+endTime+"&tranStatus="+tranStatus;
};



/**
 * 打开查看交易流水详情
 */
TransHistory.detail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '交易流水详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/transHistory/transHistory_detail/' + Trans.seItem.id
        });
        this.layerIndex = index;
    }
};



TransHistory.formParams = function() {
    var queryData = {};
    queryData['mchNo'] = $("#mchNo").val();
    queryData['orderCode'] = $("#orderCode").val();
    queryData['platOrderCode'] = $("#platOrderCode").val();
    queryData['beginTime'] = $("#beginTime").val();
    queryData['endTime'] = $("#endTime").val();
    queryData['tranStatus'] = $("#tranStatus").val();

    return queryData;
}

/**
 * 查询交易流水列表
 */
TransHistory.search = function () {
	
	TransHistory.table.refresh({query: TransHistory.formParams()});
};

$(function () {
    var defaultColunms = TransHistory.initColumn();
    var table = new BSTable(TransHistory.id, "/transHistory/list", defaultColunms);
    table.setPaginationType("server");//服务器端分页
    table.setQueryParams(TransHistory.formParams());
    TransHistory.table = table.init();
});
